import { http } from '@/utils/request'
import { mockPlugins } from '@/mock'
import type { Plugin } from '@/stores/plugin'

/**
 * 插件API服务
 */
export const pluginApi = {
  /**
   * 获取插件列表
   */
  getPlugins: () => {
    return http<Plugin[]>('/api/plugins', 'get', undefined, mockPlugins)
  },

  /**
   * 切换插件状态
   */
  togglePluginStatus: (pluginId: string, enable: boolean) => {
    return http<Plugin>(
      `/api/plugins/${pluginId}/status`,
      'put',
      { enable },
      { id: pluginId, isActive: enable } as Plugin
    )
  },

  /**
   * 删除插件
   */
  deletePlugin: (pluginId: string) => {
    return http<boolean>(
      `/api/plugins/${pluginId}`,
      'delete',
      undefined,
      true
    )
  },

  /**
   * 添加插件
   */
  addPlugin: (plugin: Plugin) => {
    return http<Plugin>(
      '/api/plugins',
      'post',
      plugin,
      plugin
    )
  }
}

// 插件状态类型
export type PluginStatus = 'enabled' | 'disabled'

// 分页请求参数
export interface PageRequest {
  currentPage: number
  pageSize: number
  offset?: number
}

// 插件列表响应
export interface PluginListResponse {
  currentPage: number
  pageSize: number
  totalRecords: number
  totalPages: number
  records: PluginDetail[]
}

// 插件详情
export interface PluginDetail {
  id: string
  name: string
  version: string
  developer: string
  email: string
  status: PluginStatus
  installTime: string
  updateTime: string
  compatibleVersion: string
  description: string
  size: number
  dependencies: PluginDependency[]
  extensionPoints: ExtensionPoint[]
  config: Record<string, any>
}

// 插件依赖
export interface PluginDependency {
  id: string
  name: string
  version: string
  installed: boolean
}

// 扩展点
export interface ExtensionPoint {
  id: string
  name: string
  description: string
}

// 插件状态更新请求
export interface PluginStatusRequest {
  status: PluginStatus
}

// 获取插件列表
export function getPluginList(params: {
  pageRequest: PageRequest
  status?: 'all' | 'enabled' | 'disabled'
  keyword?: string
}) {
  // 将 pageRequest 对象展开为扁平化的参数
  const queryParams = {
    currentPage: params.pageRequest.currentPage,
    pageSize: params.pageRequest.pageSize,
    offset: params.pageRequest.offset || 0,
    status: params.status || 'all',
    keyword: params.keyword
  }
  
  return http<PluginListResponse>('/api/v1/plugins', 'get', queryParams)
}

// 上传插件
export function uploadPlugin(file: File, autoActivate: boolean = false) {
  const formData = new FormData()
  formData.append('file', file)
  
  return http('/api/v1/plugins', 'post', formData, undefined, {
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    params: { autoActivate }
  })
}

// 更新插件状态
export function updatePluginStatus(pluginId: string, status: PluginStatus) {
  return http(`/api/v1/plugins/${pluginId}/status`, 'patch', { status })
}

// 删除插件
export function deletePlugin(pluginId: string, force: boolean = false) {
  return http(`/api/v1/plugins/${pluginId}`, 'delete', undefined, undefined, {
    params: { force }
  })
}

// 获取插件详情
export function getPluginDetail(pluginId: string) {
  return http<PluginDetail>(`/api/v1/plugins/${pluginId}`, 'get')
} 